﻿using System;
using AccelerateSensor.SuperSocket.Tools;
using SuperSocket.SocketBase;
using SuperSocket.SocketEngine;

namespace AccelerateSensor.SuperSocket
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Press ant key to start the server!");
            Console.ReadKey();
            Console.WriteLine();

            LogHelper.InitLogPath();
            LogHelper.AddLog("Instance is starting.");

            try
            {
                var bootstrap = BootstrapFactory.CreateBootstrap();
                if (!bootstrap.Initialize())
                {
                    Console.WriteLine("Failed to initialize!");
                    Console.ReadKey();
                    return;
                }

                var result = bootstrap.Start();
                Console.WriteLine("Start result: {0}", result);
                if (result == StartResult.Failed)
                {
                    Console.WriteLine("Failed to start!");
                    Console.ReadKey();
                    return;
                }

                Console.WriteLine("Press key 'q' to stop it!");
                while (Console.ReadKey().KeyChar.ToString().ToUpper() != "Q")
                {
                    Console.WriteLine();
                }

                //Stop the appServer
                bootstrap.Stop();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception happened: {0}", ex.Message);
            }

            LogHelper.AddLog("Instance is shutting down.");
            LogHelper.Stop();

            Console.WriteLine();
            Console.WriteLine("The server was stopped!");
            Console.ReadKey();
        }
    }
}
